﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using Microsoft.Extensions.DependencyInjection;
using QServices;
using QServices.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace QControlCenter.ViewModel
{
    public partial class LoginViewModel : ObservableObject
    {

        [ObservableProperty]
        private string userCode;
        [ObservableProperty]
        private string pwd;

        public Login loginForm { get; set; }

        public LoginViewModel() 
        {
            Pwd = "000000";
            UserCode = "admin";
        }

        [RelayCommand]
        public void Login()
        {

            
           

            if (string.IsNullOrWhiteSpace(UserCode))
            {
                Growl.ErrorGlobal(new GrowlInfo
                {
                    Message = $"用户名不能为空",
                    WaitTime = 6,
                    IsCustom = true,
                });

                return;
            }
            if (string.IsNullOrWhiteSpace(Pwd))
            {
                Growl.ErrorGlobal(new GrowlInfo
                {
                    Message = $"密码不能为空",
                    WaitTime = 6,
                    IsCustom = true,
                });
                AppRuntime.Logger_Error("密码不能为空");
                return;
            }

            var isok=AppRuntime.GetService().GetService<UserService>().Login(new QModels.User { Account=UserCode,Password=Pwd});
            if (!isok)
            {
                Growl.ErrorGlobal(new GrowlInfo
                {
                    Message = $"用户名或者密码不正确",
                    WaitTime = 6,
                    IsCustom = true,
                });
				AppRuntime.Logger_Error("用户名或者密码不正确");
				return;
            }

            App.Current.MainWindow = new MainWindow();
            App.Current.MainWindow.Show();





        }
        [RelayCommand]
        public void ResetPassword()
        {

        }






    }
}
